/**
 * Created by john on 2016/8/31.
 */
(function () {
    'use strict';

    angular
        .module('news.controller', [])
        .controller('NewsCtrl', NewsCtrl);

    NewsCtrl.$inject = ['$scope', '$state', '$yikeUtils', '$ionicTabsDelegate', '$ionicLoading', 'localStorageService'];
    /* @ngInject */
    function NewsCtrl($scope, $state, $yikeUtils, $ionicTabsDelegate, $ionicLoading, localStorageService) {
        var user = localStorageService.get('user');


        var page = 1;
        $scope.newData = [];


        //下拉刷新
        $scope.refresh=refresh;

        //上拉加载
        $scope.loadMore = loadMore;


        //下拉刷新
        function refresh() {
            yikeTaishan.expire(user.id,user.token)
                .then(function (data) {
                    if(data.status == 1){
                        page=1;
                        getLists();
                    }else{
                        var alertPopup = $ionicPopup.alert({
                            title: '提示',
                            template: data.result.result,
                            buttons:[{
                                text:'确定',
                                type: 'button-positive'
                            }]
                        });
                        alertPopup.then(function() {
                            localStorageService.remove('user');
                            $state.go('login')
                        });
                    }
                })
        }


        //上拉加载
        function loadMore() {
            yikeTaishan.expire(user.id, user.token)
                .then(function (data) {
                    if (data.status == 1) {
                        getLists();
                    } else {
                        var alertPopup = $ionicPopup.alert({
                            title: '提示',
                            template: data.result.result,
                            buttons: [{
                                text: '确定',
                                type: 'button-positive'
                            }]
                        });
                        alertPopup.then(function () {
                            localStorageService.remove('user');
                            $state.go('login')
                        });
                    }
                })
        }


        //宝典秘籍
        function getLists() {
            yikeTaishan.newsList().then(function (data) {
                console.log(data);


                if (data.status == 1) {

                    if (page == 1) {
                        $scope.newData = data.result.list;
                    } else {
                        $scope.newData = $scope.newData.concat(data.result.list);
                    }
                } else {
                    $scope.newData = [];
                }
                //$scope.newData = data.result.list;
                //console.log($scope.newData);
                $scope.noMoreItemsAvailable = $scope.newData.length >= Number(data.result.list.length);
                $scope.$digest();
                $scope.$broadcast('scroll.refreshComplete');
                $scope.$broadcast('scroll.infiniteScrollComplete');
                page++;
            });
        }


    }
})();
